/*
 * Copyright (c) 2014, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.omorphdb.core.index.hash;

import java.io.IOException;

import org.omorphdb.core.index.structure.Pointer;
import org.omorphdb.core.io.Appender;
import org.omorphdb.core.io.Constants;
import org.omorphdb.core.io.Resource;
import org.omorphdb.core.io.Writer;
import org.omorphdb.core.value.ValueConstants;

/**
 * Provides method to write on a provided resource elements like slots, buckets
 * and pointers, which should be used to manage a hash index.
 * 
 * @author Marco Tamburelli
 * 
 * @param <T>
 */
abstract class HashWriter<T>
{
	protected final Appender appender;
	private final Writer writer;

	HashWriter(Resource resource) throws IOException
	{
		this.appender = resource.createAppender();
		this.writer = resource.createWriter();
	}

	/**
	 * Updates the initial block containing index statistics.
	 * 
	 * @param stat
	 * @throws IOException
	 */
	void updateStatistics(Statistics stat) throws IOException
	{
		this.writer.setCursor(0);

		this.writer.writeByte(stat.algorithm());
		this.writer.writeLong(stat.filledSlotCount());
		this.writer.writeLong(stat.collisionCount());
	}

	/**
	 * Initializes the initial block containing index statistics, then return
	 * the object containing such informations.
	 * 
	 * @return
	 * @throws IOException
	 */
	Statistics appendEmptyStatistics() throws IOException
	{
		this.appender.reserve(Statistics.SIZE);
		this.appender.appendByte(ValueConstants._BYTE);
		this.appender.appendLong(0);
		this.appender.appendLong(0);

		return new Statistics(ValueConstants._BYTE, 0, 0);
	}

	/**
	 * Appends a pointer to the end of the resource and returns its
	 * representation.
	 * 
	 * @param target The pointed position within the resource.
	 * @return
	 * 
	 * @throws IOException
	 */
	Pointer appendPointer(long target) throws IOException
	{
		this.appender.reserve(Constants.SIZEOF_LONG);
		long position = this.appender.getCursor();
		this.appender.appendLong(target);

		return new Pointer(position, target);
	}

	/**
	 * Appends a slot to the end of the resource and returns its representation.
	 * 
	 * @param target The pointed position within the resource
	 */
	void appendSlot(long target) throws IOException
	{
		appendPointer(target);
	}

	/**
	 * Appends a slot to the end of the resource and returns its representation.
	 * The slot created will point to no bucket.
	 */
	void appendSlot() throws IOException
	{
		appendPointer(-1);
	}

	/**
	 * Reserves the space for the slots in a hash set index.
	 * 
	 * @param slotCount The number of available slots;
	 * @throws IOException
	 */
	public void prepareSlots(int slotCount) throws IOException
	{
		this.appender.reserve(slotCount * Constants.SIZEOF_LONG);
	}

	/**
	 * Updates the target value of a pointer.
	 * 
	 * @param pointer
	 * @throws IOException
	 */
	void updatePointer(Pointer pointer) throws IOException
	{
		this.writer.setCursor(pointer.position());
		this.writer.writeLong(pointer.target());
	}

	/**
	 * Updates the target value of a slot.
	 * 
	 * @param slot
	 * @throws IOException
	 */
	void updateSlot(Slot slot) throws IOException
	{
		updatePointer(slot);
	}

	/**
	 * Updates the target value of a slot.
	 * 
	 * @param index The index of the slot within the slot buffer.
	 * @param target The target position of a bucket.
	 * @return The updated slot.
	 * 
	 * @throws IOException
	 */
	Slot updateSlot(int index, long target) throws IOException
	{
		Slot slot = new Slot(index, target);
		updatePointer(slot);

		return slot;
	}

	/**
	 * Appends a bucket to the end of the resource and returns its
	 * representation.
	 * 
	 * @param value The matching value.
	 * @param documentTarget The document position within the storage resource
	 * @return
	 * 
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	Bucket<T> appendBucket(Object value, long documentTarget) throws IOException
	{
		T value_ = (T) value;
		this.appender.reserve(getValueSize(value_) + Constants.SIZEOF_LONG * 2);

		long bucketPosition = this.appender.getCursor();

		appendValue(value_);
		this.appender.appendLong(documentTarget);

		Pointer next = new Pointer(this.appender.getCursor(), -1);
		Bucket<T> bucket = new Bucket<>(bucketPosition, value_, documentTarget, next);

		/* The pointer to the next chained bucket should be -1 (null) */
		this.appender.appendLong(-1L);

		return bucket;
	}

	protected abstract int getValueSize(T value);

	protected abstract void appendValue(T value) throws IOException;
}
